package cn.itcast.activitylifecycle;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText et;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityResultLauncher<Intent> intentActivityResultLauncher =
                registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                        new ActivityResultCallback<ActivityResult>() {
                            @Override
                            public void onActivityResult(ActivityResult result) {
                                //此处是跳转的result回调方法
                                if (result.getData() != null && result.getResultCode() == Activity.RESULT_OK) {
                                    String backInfo = result.getData().getStringExtra("BackInfo");
                                    Toast.makeText(getApplicationContext(), backInfo, Toast.LENGTH_LONG).show();
                                } else {
                                    Toast.makeText(getApplicationContext(), "empty_not_saved",
                                            Toast.LENGTH_LONG).show();
                                }
                            }
                        });
        et = (EditText) findViewById(R.id.et);
        btn = (Button) findViewById(R.id.btn);
        Log.i("MainActivity", "调用onCreate()");
        String str = et.getText().toString();
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = et.getText().toString();
                //startActivity(intent);
                //startActivityForResult(intent, 1);
                openSecondActivity(str, intentActivityResultLauncher);
            }
        });
    }

    private void openSecondActivity(String str, ActivityResultLauncher<Intent> intentActivityResultLauncher) {
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, SecondActivity.class);
        intent.putExtra("Info", str);
        intentActivityResultLauncher.launch(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("MainActivity", "调用onStart()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("MainActivity", "调用onResume()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("MainActivity", "调用onPause()");
    }

    @Override
    protected void onStop(){
        super.onStop();
        Log.i("MainActivity", "调用onStop()");
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.i("MainActivity", "调用onDestroy()");
    }

    @Override
    protected void onRestart(){
        super.onRestart();
        Log.i("MainActivity", "调用onRestart()");
    }
}